<?php  
class ControllerModuleSurvey extends Controller {
	protected function index($setting) {
		static $module = 0;
		
		$this->load->model('survey/survey');
		
		$this->document->addScript('catalog/view/javascript/jquery/jquery.jcarousel.min.js');
		$this->document->addStyle('catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/carousel.css');
				
		$this->data['limit'] = $setting['limit'];
		$this->data['scroll'] = $setting['scroll'];
				
		$this->data['surveys'] = array();
		
		$results = $this->model_survey_survey->getSurveys();
		
//		print_r($results);  
		$surveys = array();
		
		foreach ($results as $result) {
			$this->data['surveys'][$result['survey_id']] = array(
				'id' => $result['survey_id'],
				'title' => $result['title'],
				'link'  => $this->url->link('survey/survey', 'survey_id='.$result['survey_id'])
			);
		}
		
		$survey_list = array();
		if(isset($this->session->data['survey_ides']) && !empty($this->session->data['survey_ides']))
			$survey_ides = explode(',', $this->session->data['survey_ides']);
		else
			$survey_ides =  array();
		
		if(isset($this->session->data['email']) && !empty($this->session->data['email']))
			$survey_given = $this->model_survey_survey->getSurveysDone($this->session->data['email']);
		else
			$survey_given = array();								
				
//		print_r($this->data['surveys']);
		
		foreach($survey_ides as $key => $survey_id) {
			$survey_list[] = array (
				'name' => 'Test '.($key + 1),
				'done' => (in_array($survey_id, $survey_given)) ? '1' : '0',
				'link' => $this->url->link('survey/survey', 'survey_id='.$survey_id),
			);
		}
		
//		print_r($this->session->data);
		
		if(count($survey_list) < 1) { // redirect to login page
			$this->redirect($this->url->link('account/login'));
		}
		
//		print_r($survey_list);
		
		$this->data['survey_list'] = $survey_list;
		
		$this->data['module'] = $module++; 
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/survey.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/survey.tpl';
		} else {
			$this->template = 'default/template/module/survey.tpl';
		}
		
		$this->render(); 
	}
}
?>